package com.game.server.db.dao;

import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;
import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.util.GetBeanFromResultUtil;

import javax.sql.rowset.CachedRowSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class XyUnionInfoDao {


    /**
     * 查询所有联盟
     *
     * @return
     */
    public static HashMap<Integer, XyUnionInfo> selectAll() {
        HashMap<Integer, XyUnionInfo> mapClub = new HashMap<>();
        String sql = "SELECT * FROM xy_union_info";
        CachedRowSet resultSet = SqlHelper.getResultSet(sql);
        try {
            while (resultSet.next()) {
                XyUnionInfo info = GetBeanFromResultUtil.getBean(resultSet, XyUnionInfo.class,null);
                mapClub.put(info.getId(), info);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return mapClub;
    }

    /**
     * 添加联盟
     *
     * @param info
     * @return
     */
    public static int insertInto(XyUnionInfo info) {
        int id = DataManager.getInstance().findOnlyID("xy_union_info","id");
        if (id != 0) {
            //加入创建时间
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date1=formatter.format(date);
            info.setCreatedate(Timestamp.valueOf(date1));

            String sql = "INSERT INTO xy_union_info (id,union_name,union_head,union_integral,creator_clubid,createdate) VALUES (?,?,?,?,?,?)";
            Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);
            Parameter p2 = new Parameter(Types.VARCHAR, info.getUnion_name(), ParameterType.Input);
            Parameter p3 = new Parameter(Types.VARCHAR, info.getUnion_head(), ParameterType.Input);
            Parameter p4 = new Parameter(Types.INTEGER, info.getUnion_integral(), ParameterType.Input);
            Parameter p5 = new Parameter(Types.INTEGER, info.getCreator_clubid(), ParameterType.Input);
            Parameter p6 = new Parameter(Types.TIMESTAMP, info.getCreatedate(), ParameterType.Input);
            SqlHelper.execute(sql, p1, p2, p3, p4, p5,p6);
        }
        return id;
    }

    public static XyUnionInfo findById(int id) {
        XyUnionInfo unionInfo = new XyUnionInfo();
        String cmdText = "SELECT * FROM xy_union_info WHERE id = ?";
        Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);

        CachedRowSet resultSet = SqlHelper.getResultSet(cmdText, p1);
        try {
            if (resultSet.next()) {
                unionInfo = GetBeanFromResultUtil.getBean(resultSet, XyUnionInfo.class,null);
            }
        } catch (SQLException e) {
            LogManager.getInstance().errorLog(e);
        }
        return unionInfo;
    }

    public static void del(int unionid) {
        String sql = "delete from xy_union_info where id = ?";
        String sql2 = "delete from xy_union_changes_quota where unid = ?";
        String sql3 = "delete from xy_notification_log where notification_type = 2 and cid = ?";
        Parameter p1 = new Parameter(Types.INTEGER, unionid, ParameterType.Input);
        SqlHelper.execute(sql,p1);
        SqlHelper.execute(sql2,p1);
        SqlHelper.execute(sql3,p1);
    }
}
